Thread: convert char** (c string array) to std::string[]

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    58

    convert char** (c string array) to std::string[]

    Hello
    is there fast way to convert c char** to std::string[] array without loop the char** ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Nope.

    Well there's probably some cute one-liner statement you could use from the STL, but that would only hide the loop from your code, and it wouldn't make it noticeably any faster.

    Why are you trying to optimise something which only happens once anyway?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Salem is right on both accounts. A possible one-liner:

    Code:
    std::vector<std::string> strings(cstrings, cstrings + num_cstrings);
    This uses the pointer to c-strings as iterator, which can be used for the vector constructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM